package tchk.xyz.lingyuntutorminiprogram.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import tchk.xyz.lingyuntutorminiprogram.dto.ComplaintDTO;
import tchk.xyz.lingyuntutorminiprogram.dto.ComplaintHandleDTO;
import tchk.xyz.lingyuntutorminiprogram.entity.Complaint;
import tchk.xyz.lingyuntutorminiprogram.exception.CustomException;
import tchk.xyz.lingyuntutorminiprogram.mapper.ComplaintMapper;
import tchk.xyz.lingyuntutorminiprogram.service.IComplaintService;

@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint> implements IComplaintService {
    @Override
    public IPage<ComplaintDTO> getComplaintList(Page<ComplaintDTO> page, String status) {
        return this.baseMapper.findComplaintList(page, status);
    }

    @Override
    public void handleComplaint(ComplaintHandleDTO handleDTO) {
        Complaint complaint = this.getById(handleDTO.getId());
        if (complaint == null) {
            throw new CustomException("投诉记录不存在");
        }
        // 获取当前操作的管理员信息
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        // 这里我们应该根据username查询到userId，但为简化，我们先假设可以直接获取
        // SysUser adminUser = userService.getOne(...)
        // complaint.setHandlerId(adminUser.getId());

        complaint.setStatus(handleDTO.getStatus());
        complaint.setHandleResult(handleDTO.getHandleResult());

        this.updateById(complaint);

        // TODO: 发送通知告知用户处理结果
    }
}
